<template>
  <el-form :model="dataForm" ref="dataForm" label-width="100px" size="small" :rules="rules" class="stop-role">
    <el-form-item label="职务名称:" prop="roleName">
      <el-input v-model="dataForm.roleName" placeholder="" maxlength="30" disabled></el-input>
    </el-form-item>
    <el-form-item label="职务编码:" prop="roleCode">
      <el-input v-model="dataForm.roleCode" placeholder="" maxlength="10" disabled></el-input>
    </el-form-item>
    <el-form-item label="停用原因:" prop="openCloseReason" v-if="roleData.roleStatus === 'OPEN'">
      <el-input v-model="dataForm.openCloseReason" type="textarea" placeholder="请输入停用原因" maxlength="300" show-word-limit clearable></el-input>
    </el-form-item>
    <el-form-item label="启用原因:" prop="openCloseReason" v-if="roleData.roleStatus  === 'CLOSE'">
      <el-input v-model="dataForm.openCloseReason" type="textarea" placeholder="请输入启用原因" maxlength="300" show-word-limit clearable></el-input>
    </el-form-item>
    <el-form-item style="margin-top: 10px" label-width="130px">
      <el-button type="" @click="close">
        取消
      </el-button>
      <el-button type="primary" @click="submit">
        <fa-icon class="fa-check" />保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { common } from '@/utils/common'
import { CONSTANT_OBJ } from '@/utils/constant'
import api from '../api'
export default {
  props: { roleData: Object },
  data () {
    return {
      dataForm: {
        roleId: '',
        roleName: '',
        roleCode: '',
        remark: '',
        openCloseReason: ''
      },
      rules: {
        openCloseReason: [{ required: true, message: '请输入停用原因', tigger: 'blur' }]
      }
    }
  },
  created () {
    if (this.roleData && this.roleData.roleId) {
      common.listAssign(this.dataForm, this.roleData)
    }
  },
  methods: {
    // 保存
    submit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.roleData.roleStatus === 'OPEN') {
            this.stopRole()
          } else if (this.roleData.roleStatus === 'CLOSE') {
            this.openRole()
          }
        }
      })
    },
    // 停用
    stopRole () {
      api.postCloseRoleData(this.dataForm).then(res => {
        const { responseCode, responseMsg } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message: `${responseMsg}`,
            type: 'success'
          })
          this.$emit('uppdataSuccess', 'role')
        }
      })
    },
    // 启用
    openRole () {
      api.postOpenRoleData(this.dataForm).then(res => {
        const { responseCode, responseMsg } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message: `${responseMsg}`,
            type: 'success'
          })
          this.$emit('uppdataSuccess', 'role')
        }
      })
    },
    close () {
      this.$emit('close')
    }
  }
}
</script>

<style lang="less">
.stop-role {
  .el-input.is-disabled .el-input__inner {
    color: #666;
  }
}
</style>
